package com.padakeji.android.pworks.module.image.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.padakeji.android.pworks.R;
import com.padakeji.android.pworks.content.PadaImageLoaderFactory;
import com.padakeji.android.pworks.module.image.entity.ImageDirectory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AtanL on 2015/9/18.
 */
public class DirectoryListAdapter extends BaseAdapter {

    private static class ViewHolder {
        public ImageView coverImage;
        public TextView dirName;
        public TextView imageCount;

        public ViewHolder(View itemView) {
            coverImage = (ImageView) itemView.findViewById(R.id.pworks_activity_image_picker_image_cover);
            dirName = (TextView) itemView.findViewById(R.id.pworks_activity_image_picker_text_dir_name);
            imageCount = (TextView) itemView.findViewById(R.id.pworks_activity_iamge_picker_text_image_count);
        }
    }


    private Context mContext;
    private List<ImageDirectory> imageDirectories;

    public DirectoryListAdapter(Context context, List<ImageDirectory> imageDirectories) {
        if (context == null) {
            throw new RuntimeException("A context must be passed the constructor.");
        }
        if (imageDirectories == null) {
            imageDirectories = new ArrayList<>();
        }
        this.mContext = context;
        this.imageDirectories = imageDirectories;
    }


    @Override
    public int getCount() {
        return imageDirectories.size();
    }

    @Override
    public ImageDirectory getItem(int position) {
        return imageDirectories.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.pworks_activity_image_picker_item_directory, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        ImageDirectory d = getItem(position);
        String coverImageUri = "file://" + d.coverImagePath;
        PadaImageLoaderFactory.getImageLoader(mContext).display(coverImageUri, vh.coverImage);
        vh.dirName.setText(d.name);
        vh.imageCount.setText(d.images.size() + mContext.getString(R.string.pworks_image_unit_name));

        return convertView;
    }
}
